package com.aaa.service.sas.impl;

import com.aaa.dao.sas.TSasHuiFangMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.entity.sas.TSasHuiFang;
import com.aaa.service.sas.TSasHuifangService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class TSasHuiFangServiceImpi implements TSasHuifangService {
    @Autowired
    private TSasHuiFangMapper tSasHuiFangMapper;

    @Override
    public PageInfo selectall(MyPage myPage, TSasHuiFang tSasHuiFang) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<TSasHuiFang> select=tSasHuiFangMapper.selectLike(tSasHuiFang);
        PageInfo pageInfo=new PageInfo<>(select);
        return pageInfo;
    }

    @Override
    public Integer delete(Integer id) {
        return tSasHuiFangMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean update(TSasHuiFang tSasHuiFang) {
        tSasHuiFang.setUpdatetime(new Date());
        int i=tSasHuiFangMapper.updateByPrimaryKeySelective(tSasHuiFang);
        if (i>0){
            return  true;
        }else {
            return false;
        }

    }

    @Override
    public boolean add(HttpServletRequest request, HttpSession session, TSasHuiFang tSasHuiFang) {
        User user=(User) session.getAttribute("user");
        String createby=user.getUsername();
        String recond=request.getParameter("recond");
        String remark=request.getParameter("remake");
        String username=request.getParameter("username");
        Integer rid=tSasHuiFangMapper.selectridbyusername(username);
        Integer pid=tSasHuiFangMapper.selectpidbyrid(username);
        Date createtime=new Date();
        tSasHuiFang.setRecond(recond);
        tSasHuiFang.setCreateby(createby);
        tSasHuiFang.setRemark(remark);
        tSasHuiFang.setRid(rid);
        tSasHuiFang.setPid(pid);
        tSasHuiFang.setCreatetime(createtime);
        boolean all=tSasHuiFangMapper.insertAll(tSasHuiFang);
        return all;
    }

    @Override
    public Integer selectidByusername(String username) {
        Integer id=tSasHuiFangMapper.selectidByusername(username);
        return id;
    }

    @Override
    public Integer selectusernameidByusername(Integer id) {
        Integer usernameid=tSasHuiFangMapper.selectusernameidByusername(id);
        return usernameid;
    }
}
